package com.example.simin_manage.admim.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.simin_manage.admim.dto.SysDeptVO;
import com.example.simin_manage.admim.service.SysDeptService;
import com.example.simin_manage.entity.SysDept;
import com.example.simin_manage.entity.SysUser;
import com.example.simin_manage.mapper.DeptMapper;
import com.example.simin_manage.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/14
 */



@Service
public class SysDeptServiceImpl extends ServiceImpl<DeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private SysUserMapper userMapper; // 用于查组长名字

    /**
     * 查询所有部门，附带组长名字
     */
    public List<SysDeptVO> listDeptsWithLeaderName() {
        List<SysDept> depts = this.list(); // ServiceImpl 提供的 list() 方法
        List<SysDeptVO> voList = new ArrayList<>();
        for (SysDept dept : depts) {
            SysDeptVO vo = new SysDeptVO();
            BeanUtils.copyProperties(dept, vo);
            if (dept.getLeaderId() != null) {
                SysUser leader = userMapper.selectById(dept.getLeaderId());
                vo.setLeaderName(leader != null ? leader.getNickname() : "未知");
            } else {
                vo.setLeaderName("未分配");
            }
            voList.add(vo);
        }
        return voList;
    }


}
